package org.bpms.controller.weixin;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.UUID;

import org.bpms.interceptor.GlobalInterceptor;
import org.bpms.interceptor.WebInterceptor;
import org.bpms.model.admin.Faqs;
import org.bpms.model.admin.Member;
import org.bpms.model.wechat.Weixin;
import org.bpms.model.wechat.WeixinMenu;
import org.bpms.service.admin.FaqService;
import org.bpms.service.admin.MemberService;
import org.bpms.service.wechat.WeixinMenuService;
import org.bpms.service.wechat.WeixinService;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Enhancer;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.JsTicket;
import com.jfinal.weixin.sdk.api.JsTicketApi;
import com.jfinal.weixin.sdk.api.JsTicketApi.JsApiType;
import com.jfinal.weixin.sdk.api.MenuApi;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.jfinal.weixin.sdk.jfinal.ApiController;

/**
 * 微信api控制器
 * @author hupei
 *
 */
public class WeixinApiController extends ApiController {
	private WeixinService weixinService = Enhancer.enhance(WeixinService.class);
	private WeixinMenuService weixinMenuService = Enhancer.enhance(WeixinMenuService.class);
	private MemberService     memberService = Enhancer.enhance(MemberService.class);
	private  FaqService faqService =  Enhancer.enhance(FaqService.class);
	
	@Override
	public ApiConfig getApiConfig() {
		ApiConfig ac = new ApiConfig();
		Prop p = PropKit.use("config.properties");
		Weixin weixin = weixinService.find();
		if (null != weixin){
			// 配置微信 API 相关常量
			ac.setAppId(weixin.getStr("appid"));
			ac.setAppSecret(weixin.getStr("appsecret"));
			ac.setToken(weixin.getStr("token"));
			if (null != weixin.getStr("encodingaeskey") && !weixin.getStr("encodingaeskey").equals("")){
				ac.setEncryptMessage(true);
				ac.setEncodingAesKey(weixin.getStr("encodingaeskey"));
			}
			if (p.getBoolean("redis_mode")){
				ac.setIsredis(true);
			}
			return ac;
		}
		return null;
	}

	/**
	 * 创建菜单
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonGenerationException 
	 */
	@Clear(WebInterceptor.class)
	public void createMenu() throws JsonGenerationException, JsonMappingException, IOException{
		String jsonStr = weixinMenuService.WeixinMenu(getApiConfig().getAppId());
		ApiResult apiResult = MenuApi.createMenu(jsonStr);
		if (apiResult.isSucceed())
			renderText(apiResult.getJson());
		else
			renderText(apiResult.getErrorMsg());		
	}
	
	/**
	 * 菜单跳转外部链接
	 */
	@Clear(GlobalInterceptor.class)
	public void exturl(){
		String menuid = getPara(0);
		WeixinMenu menu = weixinMenuService.findById(menuid);
		String exturl = menu.getStr("eurl");
		String appid = getApiConfig().getAppId();
		String secret= getApiConfig().getAppSecret();
		String code = getPara("code" , "");
		SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appid, secret, code);
		Member member =getSessionAttr("member");
		if (null == member){
			String openid = snsAccessToken.getOpenid();
			member = memberService.findById(openid);	
			setSessionAttr("member", member);
		}
		String url = "";
		if (exturl.contains("?")){
			url = exturl+"&openid="+snsAccessToken.getOpenid();
		}else{
			url = exturl+"?openid="+snsAccessToken.getOpenid();
		}
		redirect(url);
	}
	
	/**
	 * 首页
	 */
	@Clear(GlobalInterceptor.class)
	public void portal(){
		String type = getPara("type" , "");
		setAttr("type", 0);
		render("/faq/index.html");
	}	
	
	/**
	 * ajax问答列表
	 */
	@Clear(GlobalInterceptor.class)
	public void ajaxlist(){ 
		Map<String,Object> pagemap = new HashMap<String, Object>();
		int type = getParaToInt("type" , 0);
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 4);//每页多少条数据，默认15
		String orderField = getPara("orderField", "praise");//对什么字段排序，默认空字符串
		String orderDirection = getPara("orderDirection", "desc");//升序还是降序，默认升序
		faqService.findAllTop("", type, "", "", pageNumber, pageSize,
				orderField, orderDirection, pagemap);
		pagemap.put("type", type);
		renderJson(pagemap);
	}
	
	/**
	 * 搜索页面
	 */
	@Clear(GlobalInterceptor.class)
	public void search(){
		render("/faq/search.html");
	}
	
	/**
	 * 搜素
	 */
	@Clear(GlobalInterceptor.class)
	public void dosearch(){
		String title = getPara("title" ,"");
		setAttr("title", title);
		render("/faq/result.html");
	}
	
	/**
	 * ajax搜素
	 */
	@Clear(GlobalInterceptor.class)
	public void ajaxdosearch(){
		Map<String,Object> pagemap = new HashMap<String, Object>();
		int type = getParaToInt("type" , 0);
		String title = getPara("title" ,"");
		try {
			title = java.net.URLDecoder.decode(title , "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 4);//每页多少条数据，默认15
		faqService.findAllTop(title,type ,"","", pageNumber, pageSize, "praise", "desc", pagemap);
		pagemap.put("type", type);
		renderJson(pagemap);
	}
	
	/**
	 * 详情页面
	 */
	@Clear(GlobalInterceptor.class)
	public void detail(){
		int id = getParaToInt("id" , 0);
		String type = getPara("type" , "");
		String mid = getPara("mid" , "");
		Record record = faqService.findById(id);
		Member member =getSessionAttr("member");
		boolean ishave = faqService.ismy(id, member.getStr("id"));//是否是我的问题
		boolean praise = faqService.isFollow(id, member.getStr("id"));//是否是我关注的问题
		if (ishave){
			setAttr("ishave", 1);
		}else{
			setAttr("ishave", 0);
		}
		if (praise){
			setAttr("praise", 1);
		}else{
			setAttr("praise", 0);
		}
		setAttr("faq", record);
		setAttr("id", id);
		if (type.equals("answer") || type.equals("collection")){
			setAttr("type", type);
			setAttr("mid", mid);
			render("/faq/specialdetail.html");
		}else{
			render("/faq/detail.html");
		}
	}
	
	/**
	 * 回复详情页面
	 */
	@Clear(GlobalInterceptor.class)
	public void replydetail(){
		int id = getParaToInt("id" , 0);
		Record record = faqService.findById(id);
		Record crecord = faqService.findById(record.getInt("pid"));
		Member member =getSessionAttr("member");
		boolean ishave = faqService.ismy(id, member.getStr("id"));//是否是我的回复
		boolean praise = faqService.isPraise(id, member.getStr("id"));//是否点赞过
		boolean collection = faqService.isCollection(id, member.getStr("id"));//是否收藏过
		if (ishave){
			setAttr("ishave", 1);
		}else{
			setAttr("ishave", 0);
		}
		if (praise){
			setAttr("praise", 1);
		}else{
			setAttr("praise", 0);
		}
		if (collection){
			setAttr("collection", 1);
		}else{
			setAttr("collection", 0);
		}
		setAttr("title", crecord.getStr("title"));
		setAttr("faq", record);
		setAttr("id", id);
		render("/faq/replydetail.html");
	}
	
	/**
	 * ajax详情回复
	 */
	@Clear(GlobalInterceptor.class)
	public void ajaxdetail(){
		Map<String,Object> pagemap = new HashMap<String, Object>();
		String mid = getPara("mid" , "");
		int id = getParaToInt("id" , 0);
		String type = getPara("type" , "");
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 4);//每页多少条数据，默认15
		faqService.findAllChi(id,mid ,type, pageNumber, pageSize, "praise", "desc", pagemap);
		renderJson(pagemap);
	}
	
	/**
	 * 提问页面
	 */
	@Clear(GlobalInterceptor.class)
	public void ask(){
		render("/faq/ask.html");		
	}
	
	/**
	 * 提问
	 */
	@Clear(GlobalInterceptor.class)
	public void doask(){
		Faqs faqs = getModel(Faqs.class);
		faqService.saveOrUpdate(faqs);
		int type = faqs.getInt("typeid");
		redirect("/wechatapi/portal");
	}
	
	/**
	 * 回复页面
	 */
	@Clear(GlobalInterceptor.class)
	public void reply(){
		int id = getParaToInt("id" , 0);
		Record record = faqService.findById(id);
		setAttr("faq", record);
		setAttr("pid", id);
		render("/faq/reply.html");			
	}
	
	/**
	 * 回复问答
	 */
	@Clear(GlobalInterceptor.class)
	public void doreply(){
		Faqs faq = getModel(Faqs.class);
		faqService.saveOrUpdate(faq);
		redirect("/wechatapi/detail?id="+faq.getInt("pid"));	
	}
	
	
	/**
	 * 个人主页
	 */
	@Clear(GlobalInterceptor.class)
	public void personal(){
		String openid = getPara("mid" , "");
		Member person = memberService.findById(openid);
		Member member =getSessionAttr("member");
		int asks = memberService.getAsks(openid);//个人提问数
		int replys = memberService.getReplys(openid);//个人回复数
		int collections = memberService.getCollections(openid);//个人收藏数
		int follows = memberService.getFollows(openid);//个人关注问题数
		int praise = memberService.getPraise(openid);//个人点赞数
		int praispersons = memberService.getPraisPersons(openid);//被我关注的人
		int praisyous = memberService.getPraisYous(openid);//关注我的人
		if (!member.getStr("id").equals(person.getStr("id"))){
			boolean ispraise = memberService.isPraise(member.getStr("id"), person.getStr("id"));
			if (ispraise){
				setAttr("ispraise", 1);
			}else{
				setAttr("ispraise", 0);
			}		
		}else{
			setAttr("ispraise", 2);
		}
		setAttr("person", person);
		setAttr("asks", asks);
		setAttr("replys", replys);
		setAttr("collections", collections);
		setAttr("follows", follows);
		setAttr("praise", praise);
		setAttr("praispersons", praispersons);
		setAttr("praisyous", praisyous);
		render("/faq/home.html");
	}
	
	/**
	 * 去编辑个人信息
	 */
	@Clear(GlobalInterceptor.class)
	public void toeditperson(){
		render("/faq/autograph.html");
	}
	
	/**
	 * 编辑个人信息
	 */
	@Clear(GlobalInterceptor.class)
	public void doeditperson(){
		Member member = getModel(Member.class);
		memberService.edit(member);
		member = memberService.findById(member.getStr("id"));
		removeSessionAttr("member");
		setSessionAttr("member", member);
		render("/faq/autograph.html");
	}
	
	/**
	 * 点赞回复
	 */
	@Clear(GlobalInterceptor.class)
	public void praise(){
		Member member =getSessionAttr("member");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		if (null == member){
			String appid = getApiConfig().getAppId();
			String secret= getApiConfig().getAppSecret();
			String code = getPara("code" , "");	
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appid, secret, code);
			String openid = snsAccessToken.getOpenid();
			member = memberService.findById(openid);	
			setSessionAttr("member", member);
		}
		int id = getParaToInt("id" , 0);
		int num = getParaToInt("num" , 0);
		num = faqService.doPraise(id, member.getStr("id"));
		renderJson(num);
	}
	
	/**
	 * 取消点赞回复
	 */
	@Clear(GlobalInterceptor.class)
	public void noPraise(){
		Member member =getSessionAttr("member");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		if (null == member){
			String appid = getApiConfig().getAppId();
			String secret= getApiConfig().getAppSecret();
			String code = getPara("code" , "");	
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appid, secret, code);
			String openid = snsAccessToken.getOpenid();
			member = memberService.findById(openid);	
			setSessionAttr("member", member);
		}
		int id = getParaToInt("id" , 0);
		int num = getParaToInt("num" , 0);
		num = faqService.doNoPraise(id, member.getStr("id"));
		renderJson(num);
	}
	
	/**
	 * 收藏回复
	 */
	@Clear(GlobalInterceptor.class)
	public void collection(){
		Member member =getSessionAttr("member");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		if (null == member){
			String appid = getApiConfig().getAppId();
			String secret= getApiConfig().getAppSecret();
			String code = getPara("code" , "");	
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appid, secret, code);
			String openid = snsAccessToken.getOpenid();
			member = memberService.findById(openid);	
			setSessionAttr("member", member);
		}
		int id = getParaToInt("id" , 0);
		int num = getParaToInt("num" , 0);
		num = faqService.doCollection(id, member.getStr("id"));
		renderJson(num);		
	}
	
	/**
	 * 取消收藏回复
	 */
	@Clear(GlobalInterceptor.class)
	public void noCollection(){
		Member member =getSessionAttr("member");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		if (null == member){
			String appid = getApiConfig().getAppId();
			String secret= getApiConfig().getAppSecret();
			String code = getPara("code" , "");	
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appid, secret, code);
			String openid = snsAccessToken.getOpenid();
			member = memberService.findById(openid);	
			setSessionAttr("member", member);
		}
		int id = getParaToInt("id" , 0);
		int num = getParaToInt("num" , 0);
		num = faqService.doNoCollection(id, member.getStr("id"));
		renderJson(num);		
	}
	
	/**
	 * 关注问题
	 */
	@Clear(GlobalInterceptor.class)
	public void follow(){
		Member member =getSessionAttr("member");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		if (null == member){
			String appid = getApiConfig().getAppId();
			String secret= getApiConfig().getAppSecret();
			String code = getPara("code" , "");	
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appid, secret, code);
			String openid = snsAccessToken.getOpenid();
			member = memberService.findById(openid);	
			setSessionAttr("member", member);
		}
		int id = getParaToInt("id" , 0);
		int num = getParaToInt("num" , 0);
		num = faqService.doFollow(id, member.getStr("id"));
		renderJson(num);
	}
	
	/**
	 * 取消关注问题
	 */
	@Clear(GlobalInterceptor.class)
	public void noFollow(){
		Member member =getSessionAttr("member");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		if (null == member){
			String appid = getApiConfig().getAppId();
			String secret= getApiConfig().getAppSecret();
			String code = getPara("code" , "");	
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appid, secret, code);
			String openid = snsAccessToken.getOpenid();
			member = memberService.findById(openid);	
			setSessionAttr("member", member);
		}
		int id = getParaToInt("id" , 0);
		int num = getParaToInt("num" , 0);
		num = faqService.doNotFollow(id, member.getStr("id"));
		renderJson(num);
	}
	
	/**
	 * 关注用户
	 */
	@Clear(GlobalInterceptor.class)
	public void followperson(){
		Member member =getSessionAttr("member");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		if (null == member){
			String appid = getApiConfig().getAppId();
			String secret= getApiConfig().getAppSecret();
			String code = getPara("code" , "");	
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appid, secret, code);
			String openid = snsAccessToken.getOpenid();
			member = memberService.findById(openid);	
			setSessionAttr("member", member);
		}
		String mid = getPara("mid" , "");
		int num = getParaToInt("num" , 0);
		num = faqService.doFollowperson(mid, member.getStr("id"));
		renderJson(num);
	}
	
	/**
	 * 取消关注用户
	 */
	@Clear(GlobalInterceptor.class)
	public void noFollowperson(){
		Member member =getSessionAttr("member");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		if (null == member){
			String appid = getApiConfig().getAppId();
			String secret= getApiConfig().getAppSecret();
			String code = getPara("code" , "");	
			SnsAccessToken snsAccessToken = SnsAccessTokenApi.getSnsAccessToken(appid, secret, code);
			String openid = snsAccessToken.getOpenid();
			member = memberService.findById(openid);	
			setSessionAttr("member", member);
		}
		String mid = getPara("mid" , "");
		int num = getParaToInt("num" , 0);
		num = faqService.doNotFollowperson(mid, member.getStr("id"));
		renderJson(num);
	}
	
	/**
	 * 到达编辑提问页面
	 */
	@Clear(GlobalInterceptor.class)
	public void toEditAsk(){	
		int id = getParaToInt("id" , 0);
		Record record = faqService.findById(id);	
		setAttr("faq", record);
		render("/faq/editask.html");
	}
	
	/**
	 * 编辑提问
	 */
	@Clear(GlobalInterceptor.class)
	public void doEditAsk(){	
		Faqs faqs = getModel(Faqs.class);
		faqService.saveOrUpdate(faqs);
		int type = faqs.getInt("typeid");
		redirect("/wechatapi/detail?id="+faqs.getInt("id"));
	}
	
	/**
	 * 删除提问
	 */
	@Clear(GlobalInterceptor.class)
	public void delAsk(){
		int id = getParaToInt("id" , 0);
		Record record = faqService.findById(id);
		faqService.del(id);
		redirect("/wechatapi/portal");
	}
	
	/**
	 * 删除回复
	 */
	@Clear(GlobalInterceptor.class)
	public void delReply(){
		int id = getParaToInt("id" , 0);
		Record record = faqService.findById(id);
		int pid = record.getInt("pid");
		faqService.del(id);
		redirect("/wechatapi/detail?id="+pid);
	}
	
	/**
	 * 到达编辑回复页面
	 */
	@Clear(GlobalInterceptor.class)
	public void toEditReply(){	
		int id = getParaToInt("id" , 0);
		Record record = faqService.findById(id);	
		setAttr("faq", record);
		render("/faq/editreply.html");
	}
	
	/**
	 * 被我关注的人的页面
	 */
	@Clear(GlobalInterceptor.class)
	public void myfollowperson(){
		String mid = getPara("mid" , "");
		Member member = memberService.findById(mid);
		setAttr("person", member);
		render("/faq/gzdr.html");
	}
	/**
	 * ajax被我关注的人
	 */
	@Clear(GlobalInterceptor.class)
	public void ajaxmyfollowperson(){
		String mid = getPara("mid" , "");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 4);//每页多少条数据，默认15
		memberService.findAllFollow(mid, pageNumber, pageSize, "id", "asc", pagemap);
		renderJson(pagemap);
	}
	
	/**
	 * 关注我的人的页面
	 */
	@Clear(GlobalInterceptor.class)
	public void mybfollowperson(){
		String mid = getPara("mid" , "");
		Member member = memberService.findById(mid);
		setAttr("person", member);
		render("/faq/bgzdr.html");
	}
	/**
	 * ajax关注我的人
	 */
	@Clear(GlobalInterceptor.class)
	public void ajaxbmyfollowperson(){
		String mid = getPara("mid" , "");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 4);//每页多少条数据，默认15
		memberService.findAllBfollow(mid, pageNumber, pageSize, "id", "asc", pagemap);
		renderJson(pagemap);
	}
	
	/**
	 * 关注的问题
	 */
	@Clear(GlobalInterceptor.class)
	public void followask(){
		String mid = getPara("mid" , "");
		Member member = memberService.findById(mid);
		setAttr("person", member);
		render("/faq/guanzhu.html");
	}
	
	/**
	 * ajax关注的问题
	 */
	@Clear(GlobalInterceptor.class)
	public void ajaxfollowask(){
		String mid = getPara("mid" , "");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 4);//每页多少条数据，默认15
		faqService.findMyAllFollow(mid, pageNumber, pageSize, "praise", "desc", pagemap);
		renderJson(pagemap);
	}
	/**
	 * 我的收藏页面
	 */
	@Clear(GlobalInterceptor.class)
	public void mycollection(){
		String mid = getPara("mid" , "");
		String type = getPara("type" , "");
		Member member = memberService.findById(mid);
		setAttr("person", member);
		setAttr("type", type);
		render("/faq/shoucang.html");
	}
	
	/**
	 * ajax我的收藏页面
	 */
	@Clear(GlobalInterceptor.class)
	public void ajaxmycollection(){
		String mid = getPara("mid" , "");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 4);//每页多少条数据，默认15
		faqService.findMyAllCollection(mid, pageNumber, pageSize, "praise", "desc", pagemap);
		renderJson(pagemap);
	}	
	
	/**
	 * 我的提问页面
	 */
	@Clear(GlobalInterceptor.class)
	public void myask(){
		String mid = getPara("mid" , "");
		Member member = memberService.findById(mid);
		setAttr("person", member);
		render("/faq/tiwen.html");
	}
	
	/**
	 * ajax我的提问
	 */
	@Clear(GlobalInterceptor.class)
	public void ajaxmyask(){
		String mid = getPara("mid" , "");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 4);//每页多少条数据，默认15
		faqService.findMyAllTop(mid, pageNumber, pageSize, "praise", "desc", pagemap);
		renderJson(pagemap);
	}	
	
	/**
	 * 我的回复页面
	 */
	@Clear(GlobalInterceptor.class)
	public void myanswer(){
		String mid = getPara("mid" , "");
		String type = getPara("type" , "");
		Member member = memberService.findById(mid);
		setAttr("person", member);
		setAttr("type", type);
		render("/faq/answer.html");
	}
	
	/**
	 * ajax我的回复
	 */
	@Clear(GlobalInterceptor.class)
	public void ajaxmyanswer(){
		String mid = getPara("mid" , "");
		Map<String,Object> pagemap = new HashMap<String, Object>();
		Integer pageNumber = getParaToInt("pageCurrent", 1);//要访问第几页，默认1
		Integer pageSize = getParaToInt("pageSize", 4);//每页多少条数据，默认15
		faqService.findMyReAllTop(mid, pageNumber, pageSize, "praise", "desc", pagemap);
		renderJson(pagemap);
	}	
	
	/**
	 * 编辑提问
	 */
	@Clear(GlobalInterceptor.class)
	public void doEditReply(){	
		Faqs faqs = getModel(Faqs.class);
		faqService.saveOrUpdate(faqs);
		redirect("/wechatapi/detail?id="+faqs.getInt("pid"));
	}

	
	
	/**
	 * 页面签名认证
	 */
	@Clear
	public void findwx(){
		ApiConfigKit.setThreadLocalApiConfig(getApiConfig());
		String _wxShareUrl = getPara("url");
		String appId = ApiConfigKit.getApiConfig().getAppId();
		String appSecret = ApiConfigKit.getApiConfig().getAppSecret();

		String _wxJsapiTicket = "";
		try {
		    JsTicket jsTicket = JsTicketApi.getTicket(JsApiType.jsapi);
		    _wxJsapiTicket      = jsTicket.getTicket();
		} finally {
		    //ApiConfigKit.removeThreadLocalApiConfig();
		}

		Map<String, String> _wxMap = new TreeMap<String, String>();
		//noncestr
		String _wxNoncestr         = UUID.randomUUID().toString().replace("-", "");
		//timestamp
		String _wxTimestamp        = (System.currentTimeMillis() / 1000) + "";

		//参数封装
		_wxMap.put("noncestr", _wxNoncestr);
		_wxMap.put("timestamp", _wxTimestamp );
		_wxMap.put("jsapi_ticket", _wxJsapiTicket);
		_wxMap.put("url", _wxShareUrl);

		// 加密获取signature
		StringBuilder _wxBaseString = new StringBuilder();
		for (Entry<String, String> param : _wxMap.entrySet()) {
		    _wxBaseString.append(param.getKey()).append("=").append(param.getValue()).append("&");
		}
		String _wxSignString = _wxBaseString.substring(0, _wxBaseString.length() - 1);
		// signature
		String _wxSignature = HashKit.sha1(_wxSignString);
		_wxMap.put("signature", _wxSignature);
		_wxMap.put("appId" , appId);
		renderJson(_wxMap);
	}
}
